//
//  AirPrintTestVc.swift
//  elarc01
//
//  Created by elarc on 2016/10/14.
//  Copyright © 2016年 elarc. All rights reserved.
//

import UIKit

class AirPrintTestVc: UIViewController {
    
    var chooseBtn:UIButton?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.randomColor()
        chooseBtn = UIButton(type: .Custom)
        chooseBtn?.setTitle("打印", forState: .Normal)
        chooseBtn?.setTitleColor(UIColor.randomColor(), forState: .Normal)
        chooseBtn?.backgroundColor = UIColor.randomColor()
        chooseBtn?.frame = CGRectMake(100, 100, ScreenWidth-200, 44)
        self.view.addSubview(chooseBtn!)
        chooseBtn?.addTarget(self, action: #selector(AirPrintTestVc.printText), forControlEvents: .TouchUpInside)
    }
    
    func printText() {
        // 打印控制器
        let printController = UIPrintInteractionController.sharedPrintController()
        // 打印任务相关信息
        let printInfo = UIPrintInfo(dictionary:nil)
        printInfo.outputType = UIPrintInfoOutputType.General
        printInfo.jobName = "elarcPrint"
        printController.printInfo = printInfo
        
        //设置打印文本
        let printText = "<div style='font-size:28px;font-weight:bold;text-align:center'>"
            + "这是airprint打印</div>"
            + "<img src='http://dingyue.nosdn.127.net/s1eJ4uMhfUB0HUJMiLQVW5YC0qi=YuMkCDmpEOBfwDVbJ1474965328025.jpg' width='100%'>"
            + "哈哈哈"
            + "哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈哈哈哈和哈哈哈哈哈哈哈哈哈哈哈哈和"
        // 格式化打印文本
        let formatter = UIMarkupTextPrintFormatter(markupText: printText)
        // 设置页面Insets边距
        formatter.contentInsets = UIEdgeInsets(top: 72, left: 72, bottom: 72, right: 72)
        printController.printFormatter = formatter
        
        // 提供打印界面让用户选择打印机和副本的数量
        printController.presentAnimated(true, completionHandler: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}